import { Injectable } from '@angular/core';
import { RestService, PagedResultDto, PagedAndSortedResultRequestDto } from '@abp/ng.core';
import { CreateUpdateOrderDto, OrderDto } from './models';
import { Observable } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class OrdersService {
  apiName = 'Default';

  constructor(private restService: RestService) {}

  create(input: CreateUpdateOrderDto): Observable<OrderDto> {
    return this.restService.request<any, OrderDto>({
      method: 'POST',
      url: '/api/app/orders',
      body: input
    });
  }

  update(id: string, input: CreateUpdateOrderDto): Observable<OrderDto> {
    return this.restService.request<any, OrderDto>({
      method: 'PUT',
      url: `/api/app/orders/${id}`,
      body: input
    });
  }

  delete(id: string): Observable<void> {
    return this.restService.request<any, void>({
      method: 'DELETE',
      url: `/api/app/orders/${id}`
    });
  }

  get(id: string): Observable<OrderDto> {
    return this.restService.request<any, OrderDto>({
      method: 'GET',
      url: `/api/app/orders/${id}`
    });
  }

  getList(input: PagedAndSortedResultRequestDto): Observable<PagedResultDto<OrderDto>> {
    return this.restService.request<any, PagedResultDto<OrderDto>>({
      method: 'GET',
      url: '/api/app/orders',
      params: {
        sorting: input.sorting,
        skipCount: input.skipCount,
        maxResultCount: input.maxResultCount
      }
    });
  }
}